package cn.kgc.service;

import cn.kgc.client.RestDmOrderClient;
import cn.kgc.pojo.DmOrder;
import cn.kgc.pojo.DmOrderLinkUser;
import cn.kgc.utils.Constants;
import cn.kgc.utils.EmptyUtils;
import com.netflix.discovery.converters.Auto;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RabbitMqConsumer {


    @Autowired
    RestDmOrderClient restDmOrderClient;


    @RabbitListener(queues= Constants.RabbitQueueName.TO_UPDATE_ORDER_STATE)
    public void resetLinkedUser(String out_trade_no, Message message, Channel channel) throws IOException {
        try {
            java.util.Map<String,Object> map =new HashMap<>();
            map.put("orderNo",out_trade_no);
            DmOrder dmOrder = restDmOrderClient.getDmOrderListByMap(map).get(0);
            if(EmptyUtils.isNotEmpty(dmOrder.getAliTradeNo())){
             //需要重置的是支付宝的支付的订单数据
                dmOrder.setAliTradeNo("");
                dmOrder.setPayType("");
                dmOrder.setUpdatedTime((new java.util.Date()));
            }else{
                //需要重置的是微信的支付的订单数据
                dmOrder.setWxTradeNo("");
                dmOrder.setPayType("");
                dmOrder.setUpdatedTime(new java.util.Date());
            }
            dmOrder.setOrderType(Constants.OrderStatus.TOPAY);
            Integer result = restDmOrderClient.qdtxModifyDmOrder(dmOrder);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }
    }




}
